package com.miyake.client.datamanage;

import java.util.List;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.datamanage.plugin.ChartPluginDialog;
import com.miyake.client.datamanage.plugin.PluginProperty;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public abstract class PlotDialog extends OkCancelDialog {

	private ListBox comboX;
	private ListBox comboY;
	private TextBox textBox;
	//private Integer columnCount;
	abstract protected Integer getColumnCount();
	
//	public Integer getColumnCount() {
//		return this.columnCount;
//	}

	
	private VerticalPanel getPanel() {
		setHTML("Chart");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		//setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		
		createPluginPanel(verticalPanel);
		
		textBox = new TextBox();
		textBox.setText(getDefaultName());
		verticalPanel.add(textBox);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		
		Label lblNewLabel = new Label("X Column");
		horizontalPanel.add(lblNewLabel);
		Label lblYColumn = new Label("Y Column");
		horizontalPanel_2.add(lblYColumn);
		
		comboX = new ListBox();
		comboY = new ListBox();
		horizontalPanel.add(comboX);
		horizontalPanel_2.add(comboY);
		
		comboX.addItem("None");
		comboY.addItem("None");
		for (int i = 0; i < getColumnCount(); i++) {
			comboX.addItem("C" + (i+1));
			comboY.addItem("C" + (i+1));
		}
		comboY.setSelectedIndex(1);
		
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		
		comboX.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {

			}
		});
		comboY.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {

			}
		});
		
		return verticalPanel;
	}

	protected void createPluginPanel(VerticalPanel verticalPanel) {
		HorizontalPanel pluginPanel = new HorizontalPanel();
		verticalPanel.add(pluginPanel);
		final ListBox pluginList = new ListBox();
		pluginPanel.add(pluginList);
		pluginPanel.add(new SmallButton("Plugin", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showPluginDialog();
			}
		}));
		
		DoctorsUserService.Util.getInstance().getChartPluginList(new AsyncCallback<List<PluginProperty>>() {
			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(List<PluginProperty> result) {
				for (PluginProperty p : result) {
					pluginList.addItem(p.getName());
				}
			}
		});
	}

	protected void showPluginDialog() {
		new ShowDialog() {

			@Override
			protected void onOK(String text) {

			}

			@Override
			protected DialogBox getDialogBox() {
				return new ChartPluginDialog();
			}
			
		};
	}

	protected String getDefaultName() {
		return "Chart";
	}

	public Integer getXcol() {
		return comboX.getSelectedIndex() - 1;
	}

	public void setXcol(Integer xcol) {
		this.comboX.setSelectedIndex(xcol + 1);
	}

	public Integer getYcol() {
		return comboY.getSelectedIndex() - 1;
	}

	public void setYcol(Integer ycol) {
		this.comboY.setSelectedIndex(ycol + 1);
	}

	public String getName() {
		return this.textBox.getText();
	}

	public void setName(String name) {
		this.setText(name);
	}

	
	@Override
	protected Widget getContent() {
		return getPanel();
	}

}
